//
//  NewFeatureController.swift
//  WeiBo
//
//  Created by 陈贵 on 15/9/10.
//  Copyright © 2015年 hzj. All rights reserved.
//

import UIKit

private let reuseIdentifierID = "HZJCell"
private let theImageCount = 4


class NewFeatureController: UICollectionViewController {
    
    
    
    init(){
        super.init(collectionViewLayout: UICollectionViewFlowLayout())
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        //注册cell
        self.collectionView!.registerClass(HZJcollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifierID)
        
        prepareTheLayout()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    private func prepareTheLayout(){
        
        let layout = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = view.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        
    }
    
    


    // MARK: UICollectionViewDataSource



    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return theImageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifierID, forIndexPath: indexPath) as! HZJcollectionViewCell
        cell.backgroundColor = UIColor.redColor()
        
        cell.imageIndex = indexPath.item
        
        return cell
    }

    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath){
        
        let path = collectionView.indexPathsForVisibleItems().last!
        
        if  path.item == theImageCount - 1  {
            let cell = collectionView.cellForItemAtIndexPath(path) as! HZJcollectionViewCell
            cell.setAnimation()
        }

    }
}
/**
*  自定义cell
*/

private class HZJcollectionViewCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setUI()
    }
    
    
    func setUI(){
        addSubview(imageView)
        addSubview(startBth)
        
        imageView.frame = bounds
        
        startBth.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: startBth, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: startBth, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -88))

        
        
    }
    
    //MARK:- 懒加载
    lazy var imageView:UIImageView = UIImageView()
    lazy var startBth:UIButton = {
        
        let bth = UIButton()
        bth.setTitle("去吧！皮卡丘", forState: UIControlState.Normal)
        bth.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        bth.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        bth.titleLabel?.font = UIFont.systemFontOfSize(14)
        bth.sizeToFit()
        bth.addTarget(self, action: "clickTheBth", forControlEvents: UIControlEvents.TouchUpInside)
        return bth
    }()
    
    var imageIndex:Int = 0{
        didSet{
            imageView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startBth.hidden = true
        }
    }
    
    //按钮动画
    func setAnimation(){
        
        startBth.hidden = false
        
        startBth.transform = CGAffineTransformMakeScale(0, 0)

        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            
            self.startBth.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
        }
    }
    
    //点击按钮
    @objc func clickTheBth(){
        NSNotificationCenter.defaultCenter().postNotificationName(HZJselectTheControllerNotification, object: nil)
        
    }
    
    
}


